var util = require('../../utils/util.js');

var app = getApp();

Page({
  data: {
    
      id: '',
      provinceName: '',
      cityName: '',
      countyName: '',
      provinceId: 0,
      cityId: 0,
      countyId: 0,
      detailInfo: '',
      userName: '',
      mobile: '',    
    
    isDefault: 1,
    addressId: '',
    region: ['安徽省', '合肥市', '蜀山区']
  },

  bindinputTelNumber(event) {    
    this.setData({
      mobile: event.detail.value
    });
  },

  bindinputDetailInfo(event) {  
    this.setData({
      detailInfo: event.detail.value
    });
  },
  bindinputUserName(event) {
    this.setData({
      userName: event.detail.value
    });
  },

  bindIsDefault(e) {
    let that = this;
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data
        wx.request({
          url: app.globalData.URL + '/app/personal/replaceDefault?id=' + that.data.addressId + '&openid=' + openid,
          success: function (res) {
            console.log("设置默认地址", res.data)
            if(res.data.flag ==true){
              wx.showToast({
                title: '设置成功',
              })
              that.setData({
                isDefault:0
              })
            }
            
          }
        })
      },
    })
    
  },

  getAddressDetail(addressId) {
    let that = this;
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data

        wx.request({
          url: app.globalData.URL + '/app/personal/openAddress?id=' + addressId + '&openid=' + openid,
          success: function (res) {
            console.log(res.data)

            that.setData({
              userName: res.data.delivery.name,
              mobile: res.data.delivery.telPhone,
              detailInfo: res.data.delivery.detailed,
              provinceName: res.data.delivery.province,
              cityName: res.data.delivery.city,
              countyName: res.data.delivery.area,
              isDefault: res.data.delivery.defaultAddress,
              id: res.data.delivery.id
            });
            // that.initRegion();
          }
        })
      },
    })
    
  },

  onLoad: function (options) {
    let that = this;
    console.log(options)
    // 页面初始化 options为页面跳转所带来的参数
    if (options.id && options.id != "0") {
      that.setData({
        addressId: options.id
      });
      that.getAddressDetail(that.data.addressId);
    }

    that.initRegion();
  },

  cancelAddress() {
    wx.navigateBack({})
  },

  saveAddress() {    
    var that = this

    if (that.data.userName == '') {
      util.showErrorToast('请输入姓名');

      return false;
    }

    if (that.data.mobile == '') {
      util.showErrorToast('请输入手机号码');
      return false;
    }

    if (that.data.region.length == 0) {
      util.showErrorToast('请输入省市区');
      return false;
    }

    if (that.data.detailInfo == '') {
      util.showErrorToast('请输入详细地址');
      return false;
    }

    
    var openid = wx.getStorageSync('openid')
    var data = 
    {
      id:that.data.id || '',
      name: that.data.userName,			
      telPhone: that.data.mobile,
      detailed: that.data.detailInfo,	
      zipCode:'',			
      defaultAddress: that.data.isDefault,
      userId:'',
      version:'',
      province: that.data.provinceName,
      city: that.data.cityName,
      area: that.data.countyName	
    }
    var addr = {
      "addr":data,
      'openid':openid
    }

    console.log("参数",addr)
    wx.request({
      url: app.globalData.URL + '/app/personal/updateAddress',
      method: 'post',
      data: JSON.stringify(addr) ,
      header: {
        "Content-Type": "application/json;charset=utf-8"
      },
      success: function (res) {
        console.log(res.data)       
        wx.showToast({
          title: '',
          success:function(){
            wx.navigateBack({
              
            })
          }
        })
      }
    })    
  },
  
  /**
   * 初始化省市区
   */
  initRegion() {
    
    this.setData({
      region: [this.data.provinceName, this.data.cityName, this.data.countyName]
    });
    
  },
  
  bindRegionChange: function (e) {
    
    this.setData({
      provinceName: e.detail.value[0],
      cityName: e.detail.value[1],
      countyName: e.detail.value[2]
    })
  }
})
